<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-row :gutter="5" style="padding-top: 15px">
          <!--           左     -->
          <el-col :span="8" align="left">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['jbxx:product:add']"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['jbxx:product:edit']"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['jbxx:product:remove']"
                >删除</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['jbxx:product:export']"
                >导出</el-button>
              </el-col>
            </el-row>
          </el-col>
          <!--           右     -->
          <el-col :span="16" align="right" >
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

              <el-form-item  prop="isUse">
                <el-select v-model="queryParams.isUse" placeholder="按启用的状态筛选" clearable>
                  <el-option value="1" label="启用" key="1"></el-option>
                  <el-option value="0" label="停用" key="0"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item  prop="cpName">
                <el-input
                  v-model="queryParams.cpName"
                  placeholder="请输入产品名称"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>

        <el-table border stripe v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" type="index" :index="indexMethod" />
          <el-table-column label="产品编码" width="180" align="center" prop="cpCode" />
          <el-table-column label="产品名称" align="center" prop="cpName" />
          <el-table-column label="产品品牌" align="center" prop="ppName" >
            <template #default="scope">
              <!-- 使用v-for结合文本显示 -->
              <span v-for="(ksValue, index) in scope.row.cpppProductBrandList
" :key="index">
                {{ index > 0 ? '| ' : '' }}{{ ksValue.ppName}}
             </span>
            </template>
          </el-table-column>
          <el-table-column label="产品分类" align="center" prop="cpflName" >
            <template #default="scope">
              <!-- 使用v-for结合文本显示 -->
              <span v-for="(ksValue, index) in scope.row.cpflProductClassificationList

" :key="index">
                {{ index > 0 ? '| ' : '' }}{{ ksValue.cpflName}}
             </span>
            </template>
          </el-table-column>
          <el-table-column label="产品型号" align="center" prop="cpType" />
          <el-table-column label="单位" align="center" prop="dwName" >
            <template #default="scope">
              <!-- 使用v-for结合文本显示 -->
              <span v-for="(ksValue, index) in scope.row.jldwMeasurementUnitList

" :key="index">
                {{ index > 0 ? '| ' : '' }}{{ ksValue.dwName}}
             </span>
            </template>
          </el-table-column>
          <el-table-column label="产品类型" align="center" prop="cpLxcode">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.product_type" :value="scope.row.cpLxcode"/>
            </template>
          </el-table-column>
          <el-table-column label="关联物料" align="center" prop="wlName" >
            <template #default="scope">
              <!-- 使用v-for结合文本显示 -->
              <span v-for="(ksValue, index) in scope.row.wldaMaterialFileList

" :key="index">
                {{ index > 0 ? '| ' : '' }}{{ ksValue.wlName}}
             </span>
            </template>
          </el-table-column>
          <el-table-column label="启用" align="center" key="isUse" v-if="columns[0].visible">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isUse"
                active-value="true"
                inactive-value="false"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['jbxx:product:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['jbxx:product:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-row :gutter="5">
          <el-col :span="24" align="right">
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="10">
          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品名称" prop="cpName">
              <el-input v-model="form.cpName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品品牌" prop="cptypeCode">
              <el-input @click.native="openCpTypeCode" v-model="form.pp_name" placeholder="请选择" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品分类" prop="cpTypecode">
              <el-input @click.native="openCpTyCode" v-model="form.cpfl_name
" placeholder="请选择" disabled/>
            </el-form-item>
          </el-col>

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品型号" prop="cpType">
              <el-input   v-model="form.cpType
" placeholder="请输入" />
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="10">

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品单位" prop="dwCode">
              <el-input @click.native="openDwCode"  v-model="form.dw_name
" placeholder="请选择" disabled/>
            </el-form-item>
          </el-col>

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="产品类型" prop="cpLxcode">
              <el-select v-model="form.cpLxcode" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.product_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">

          <el-col :span="12">
            <el-form-item label="关联物料" prop="wlCode">
              <el-input @click.native="openWlCode"  v-model="form.wl_name" placeholder="请选择"  disabled/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-divider content-position="center">产品属性</el-divider>
        <el-table border  :data="sxProductAttributeList"  :row-class-name="rowSxProductAttributeIndex" @selection-change="handleSxProductAttributeSelectionChange" ref="sxProductAttribute">
          <el-table-column label="序号" align="center" prop="index" width="100"/>

          <el-table-column label="属性名称" prop="sxName" align="center">
            <template #default="scope">
              <!-- 使用v-for结合文本显示 -->
              <span v-for="(ksValue, index) in scope.row.sxAttributeValueList

" :key="index">
                {{ index > 0 ? '| ' : '' }}{{ ksValue.sxName}}
             </span>
            </template>
          </el-table-column>

          <el-table-column label="属性值" prop="sxValue">
            <template #default="scope">
              <el-input v-model="scope.row.sxValue"  @click.native="openSXCode(scope.row)" placeholder="请选择" disabled/>
            </template>
          </el-table-column>

        </el-table>
      </el-form>

      <div slot="footer" class="dialog-footer" style="padding-top: 30px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--     选择产品品牌    -->
    <el-dialog title="选择产品品牌" :visible.sync="openCpTypeCodeTow">

      <el-form :model="queryParamsOne" ref="queryFormOne" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item  prop="ppName">
                <el-input
                  v-model="queryParamsOne.ppName"
                  placeholder="请输入品牌名称"
                  clearable
                  @keyup.enter.native="handleQueryChangeBrand"
                />
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="16">
            <div align="right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryChangeBrand" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

      </el-form>
<!--@selection-change="handleSelectionChangeBrand"-->
      <el-table border stripe :data="brandList" ref="multipleTableBrand" @cell-dblclick="handleDblClickRowBrand">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column label="产品品牌编码" align="center" prop="cptypeCode" />
        <el-table-column label="品牌名称" align="center" prop="ppName" />
        <el-table-column label="品牌简称" align="center" prop="ppEasy" />
        <el-table-column label="英文名称" align="center" prop="usaName" />
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right" style="padding-top: 30px">
          <pagination
            v-show="total0ne>0"
            :total="total0ne"
            :page.sync="queryParamsOne.pageNum"
            :limit.sync="queryParamsOne.pageSize"
            @pagination="getListBrand"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelBrand" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormBrand">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>


    <!--     选择产品分类    -->
    <el-dialog title="选择产品分类" :visible.sync="openCpTyCodeThree">

      <el-form :model="queryParamsTwo" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item  prop="cpflName">
                <el-input
                  v-model="queryParamsTwo.cpflName"
                  placeholder="请输入产品分类名称"
                  clearable
                  @keyup.enter.native="handleQueryChangeCpfl"
                />
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="16">
            <div align="right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryChangeCpfl" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

      </el-form>
<!--@selection-change="handleSelectionChangeCpfl"-->
      <el-table border stripe :data="sxAttributeValueCpflList" ref="multipleTableCpfl"  @cell-dblclick="handleDblClickRowCpfl">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column property="cpflCode" label="产品分类编码" ></el-table-column>
        <el-table-column property="cpflName" label="产品分类名称"></el-table-column>
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right" style="padding-top: 30px">
          <pagination
            v-show="totalTwo>0"
            :total="totalTwo"
            :page.sync="queryParamsTwo.pageNum"
            :limit.sync="queryParamsTwo.pageSize"
            @pagination="getCpflList"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelCpfl" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormCpfl">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>

    <!--     换算单位    -->
    <el-dialog title="选择计量单位" :visible.sync="openDwCodeFact">

      <el-form :model="queryParamsThree" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item  prop="dwName">
                <el-input
                  v-model="queryParamsThree.dwName"
                  placeholder="请输入单位名称"
                  clearable
                  @keyup.enter.native="handleQueryListFactory"
                />
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="16">
            <div align="right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryListFactory" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

      </el-form>

<!--@selection-change="handleSelectionChangeFactory"-->
      <el-table  border stripe :data="jldwMeasurementUnitList" ref="multipleTableFactory" @cell-dblclick="handleDblClickRowJldw">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column property="dwCode" label="单位编码" width="150"></el-table-column>
        <el-table-column property="dwName" label="单位名称" width="200"></el-table-column>
        <el-table-column label="单位类型编码" align="center" prop="dwTypecode">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.jsdw_unit_type" :value="scope.row.dwTypecode"/>
          </template>
        </el-table-column>
        <el-table-column property="dwEasy" label="单位简写"></el-table-column>
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right" style="padding-top: 30px">
          <pagination
            v-show="totalThree>0"
            :total="totalThree"
            :page.sync="queryParamsThree.pageNum"
            :limit.sync="queryParamsThree.pageSize"
            @pagination="getListJLDW"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelFactory" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormFactory">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>



    <!--     物料档案    -->
    <el-dialog title="选择物料" :visible.sync="openWlCodeWsk">

      <el-form :model="queryParamsFour" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item  prop="wlName">
                <el-input
                  v-model="queryParamsFour.wlName"
                  placeholder="请输入物料名称"
                  clearable
                  @keyup.enter.native="handleQueryListwlCode"
                />
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="16">
            <div align="right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryListwlCode" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

      </el-form>
<!--@selection-change="handleSelectionChangewlCode"-->
      <el-table  border stripe :data="wldaMaterialFileList" ref="multipleTablewlCode"  @cell-dblclick="handleDblClickRowWl">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column property="wlCode" label="物料编码" width="150"></el-table-column>
        <el-table-column property="wlName" label="物料名称" width="200"></el-table-column>
        <el-table-column label="物料分类" align="center" prop="wlSortcode">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.wl_sortcode" :value="scope.row.wlSortcode"/>
          </template>
        </el-table-column>
        <el-table-column property="wlGg" label="物料规格"></el-table-column>
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right" style="padding-top: 30px">
          <pagination
            v-show="totalFour>0"
            :total="totalFour"
            :page.sync="queryParamsFour.pageNum"
            :limit.sync="queryParamsFour.pageSize"
            @pagination="getListWlMess"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelWlCode" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormWlCode">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>


    <!--     属性值    -->
    <el-dialog title="选择属性值" :visible.sync="openSXCodeWsk">

      <el-form :model="queryParamsFive" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      </el-form>
<!--      @selection-change="handleSelectionChangeSXCode"-->
      <el-table  border stripe :data="sxAttributeValueList" ref="multipleTableSXCode" @row-dblclick="handleDblClickRowSXCode">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column prop="sxValue" label="属性值" />
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right" style="padding-top: 30px">
          <pagination
            v-show="totalFive > 0"
            :total="totalFive"
            :page.sync="queryParamsFive.pageNum"
            :limit.sync="queryParamsFive.pageSize"
            @pagination="getListSxCode"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelSXCode" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormSXCode">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>

  </div>
</template>

<script>
import {listProduct,listProductSx, getProduct, delProduct, addProduct, updateProduct, updateAttributeIsUse, listCpflBy,listCpflCodeBy} from "@/api/jbxx/product";
import {listBrand} from "@/api/jbxx/brand";
import {listAttributeCpfl} from "@/api/jbxx/attribute";
import {listUnit} from "@/api/jbxx/unit";
import {listWlMess} from "@/api/jbxx/wlMess";
import item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "Product",
  dicts: ['product_type', 'attribute_type', 'jsdw_unit_type', 'wl_sortcode'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSxProductAttribute: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total0ne:0,
      totalTwo:0,
      totalThree:0,
      totalFour:0,
      totalFive:0,
      // 产品管理表格数据
      productList: [],
      // 属性值表格数据
      sxProductAttributeList: [],
      sxProductAttributeTypeList: [],
      sxProductAttributeTypeListZid: [],
      sxProductAttributeTypeListCpSxId: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cpName: null,
        cpTypecode:null,
        cpCode: null,
        isUse: null,
      },
      queryParamsOne : {
        pageNum: 1,
        pageSize: 10,
        ppName: null,
        isStatus: null,
        merchantsName:null
      },
      queryParamsTwo : {
        pageNum: 1,
        pageSize: 10,
        cpflName: null,
      },
      queryParamsThree : {
        pageNum: 1,
        pageSize: 10,
        dwName: null,
      },
      queryParamsFour : {
          pageNum: 1,
          pageSize: 10,
          wlName: null,
          wlSortcode: null,
          wlGg: null,
          wlFormcode: null,
          dwCode: null,
          wlTypecode: null,
          isUse: null,
          isDelete: null,
          wlCreate: null,
          wlCreatetime: null,
          wlUpdatetime: null
      },
      queryParamsFive: {
        pageNum: 1,
        pageSize: 10,
        zId: null,
        sxValue: null,
        sxCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cpCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" }
        ],
        cpName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
          { max: 20, message: "产品名称长度不能超过20个字符", trigger: "blur"}
        ],
        cptypeCode: [
          { required: true, message: "产品品牌不能为空", trigger: "blur" }
        ],
        cpTypecode: [
          { required: true, message: "产品分类不能为空", trigger: "blur" }
        ],
        cpType: [
          { required: true, message: "产品型号不能为空", trigger: "blur" }
        ],
        dwCode: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        cpLxcode: [
          { required: true, message: "产品类型不能为空", trigger: "change" }
        ],
      },
      // 列信息
      columns: [
        { key: 0, label: `启用`, visible: true },
      ],
      //品牌
      openCpTypeCodeTow:false,
      //分类
      openCpTyCodeThree:false,
      //单位
      openDwCodeFact:false,
      //物料
      openWlCodeWsk:false,
      //属性
      openSXCodeWsk:false,
      // 产品品牌表格数据
      brandList: [],
      // 产品分类值添加表格数据
      sxAttributeValueCpflList: [],
      //计量单位
      jldwMeasurementUnitList: [],
      //物料
      wldaMaterialFileList: [],
      //属性
      sxAttributeValueList: [],
      // 选中
      selectedRow:null,
      selectedValue:null,
      selectedIndex:-1,
    };
  },
  created() {
    this.getList();
    this.getListBrand();
    this.getCpflList();
    this.getListJLDW();
    this.getListWlMess();
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        for (let i = 0; i < response.rows.length; i++) {
          response.rows[i].isUse = response.rows[i].isUse === 1 ? 'true' : 'false';
        }
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 物料档案 */
    getListWlMess() {
      listWlMess(this.queryParamsFour).then(response => {
        this.wldaMaterialFileList = response.rows;
        this.totalFour = response.total;
      });
    },
    /** 查询属性值列表 */
    getListSxCode() {
      listProductSx(this.queryParamsFive).then(response => {
        this.sxAttributeValueList = response.rows;
        console.log(this.sxAttributeValueList)
        this.totalFive = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cpCode: null,
        cpName: null,
        cptypeCode: null,
        cpTypecode: null,
        cpType: null,
        dwCode: null,
        cpLxcode: null,
        sxCode: null,
        isUse: null,
        isDelete: null,
        cpCreate: null,
        cpCreatetime: null,
        cpUpdatetime: null
      };
      this.sxProductAttributeList = [];
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cpCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增产品管理";
      this.queryParamsOne.ppName = null;
      this.queryParamsTwo.cpflName = null;
      this.queryParamsThree.dwName = null;
      this.queryParamsFour.wlName = null;
      this.selectedRow = null;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cpCode = row.cpCode || this.ids;
      getProduct(cpCode).then(response => {
        this.queryParams.cpTypecode = response.data.cpTypecode;
        this.queryParams.cpCode = response.data.cpCode;

        listCpflCodeBy(this.queryParams).then(response => {

          this.sxProductAttributeList = response.rows;

          for (let i = 0; i < this.sxProductAttributeList.length; i++) {
            for (let j = 0; j < this.sxProductAttributeList[i].sxAttributeValueList.length; j++) {
                this.sxProductAttributeList[i].sxValue = this.sxProductAttributeList[i].sxAttributeValueList[j].sxValue;
            }
          }
        });

        this.form = response.data;
        this.open = true;
        this.queryParamsOne.ppName = null;
        this.queryParamsTwo.cpflName = null;
        this.queryParamsThree.dwName = null;
        this.queryParamsFour.wlName = null;
        this.title = "编辑产品管理";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {

        if (valid) {

          if (this.form.wlCode==null){
            this.$message.error("物料不能为空，请选择！")
            return false;
          }

          for (let i = 0; i < this.sxProductAttributeList.length; i++) {
            const row = this.sxProductAttributeList[i];
            if (!row.sxValue) {
              // 如果发现空的属性值，显示提示信息并退出函数
              this.$message({
                message: '第 ' + (i + 1) + ' 行的属性值不能为空，请填写完整后再提交。',
                type: 'error'
              });
              return;
            }
          }

          /** 属性值信息 */
          this.form.sxAttributeValueList = this.sxProductAttributeList;

          if (this.form.cpCode != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cpCodes = row.cpCode || this.ids;
      this.$modal.confirm('是否确认删除产品管理编号为"' + cpCodes + '"的数据项？').then(function() {
        return delProduct(cpCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 属性值序号 */
    rowSxProductAttributeIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleSxProductAttributeSelectionChange(selection) {
      this.checkedSxProductAttribute = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jbxx/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 搜索产品品牌按钮操作 */
    handleQueryChangeBrand() {
      this.queryParamsOne.pageNum = 1;
      this.getListBrand();
    },

    /** 搜索产品品牌按钮操作 */
    handleQueryChangeCpfl() {
      this.queryParamsTwo.pageNum = 1;
      this.getCpflList();
    },

    /** 查询产品品牌列表 */
    getListBrand() {
      listBrand(this.queryParamsOne).then(response => {
        this.brandList = response.rows;
        this.total0ne = response.total;
      });
    },

    /** 查询分类值列表 */
    getCpflList() {
      listAttributeCpfl(this.queryParamsTwo).then(response => {
        this.sxAttributeValueCpflList = response.rows;
        this.totalTwo = response.total;
      });
    },


    /** 产品品牌弹框 */
    openCpTypeCode(){
      this.openCpTypeCodeTow = true;
      this.open = false;
      // this.$nextTick(() => {
      //   if (this.form.pp_name != null) {
      //     this.selectRowCpTypeCode(this.form.pp_name);
      //   }
      // });
    },

    /** 产品分类弹框 */
    openCpTyCode(){
      this.openCpTyCodeThree = true;
      this.open = false;
      // this.$nextTick(() => {
      //   if (this.form.cpfl_name != null) {
      //     this.selectRowById(this.form.cpfl_name);
      //   }
      // });
    },

    /** 计量单位弹框 */
    openDwCode(){
      this.openDwCodeFact= true;
      this.open = false;
      // this.$nextTick(() => {
      //   if (this.form.dw_name != null) {
      //     this.selectRowDwCode(this.form.dw_name);
      //   }
      // });
    },

    /** 物料档案弹框 */
    openWlCode(){
      this.openWlCodeWsk = true;
      this.open = false;
      // this.$nextTick(() => {
      //   if (this.form.wl_name != null) {
      //     this.selectRowWLCode(this.form.wl_name);
      //   }
      // });
    },

    /** 产品属性值弹框 */
    openSXCode(row){
      this.openSXCodeWsk = true;
      this.open = false;
      this.selectedRow = row;

      for (let i = 0; i < row.sxAttributeValueList.length; i++) {
        this.queryParamsFive.sxCode =  row.sxAttributeValueList[i].sxCode;
      }

      this.getListSxCode();
      // this.$nextTick(() => {
      //   if (this.form.sxName != null) {
      //     this.selectRowSXCode(this.form.sxName);
      //   }
      // });
    },

    /** 产品品牌多选框选中数据 */
    handleSelectionChangeBrand(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTableBrand.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTableBrand.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      if (this.title == "新增产品管理") {
        const updatedSelection = this.$refs.multipleTableBrand.selection;
        for (let i = 0; i < updatedSelection.length; i++) {
          this.form.cptypeCode = updatedSelection[i].cptypeCode;
          this.form.pp_name = updatedSelection[i].ppName;
        }
      } else {
        for (let i = 0; i < selection.length; i++) {
          this.form.cptypeCode = selection[i].cptypeCode;
          this.form.pp_name = selection[i].ppName;
        }
      }
    },

    /** 产品分类多选框选中数据 */
    handleSelectionChangeCpfl(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTableCpfl.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTableCpfl.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      if (this.title == "新增产品管理") {
        for (let i = 0; i < selection.length; i++) {
          this.form.cpTypecode = selection[i].cpflCode;
          this.form.cpfl_name = selection[i].cpflName;
        }
      } else {
        for (let i = 0; i < selection.length; i++) {
          this.form.cpfl_name = selection[i].cpflName;
        }
      }
    },


    /**产品品牌分类取消*/
    cancelBrand() {
      this.openCpTypeCodeTow=false;
      this.open = true;
      this.$refs.multipleTableCpfl.clearSelection();
      if (this.title=="新增产品管理"){
        if (this.form.cpfl_name != null){
          this.selectedRow = null;
          this.form.cpfl_name=undefined
        }
      }
    },

    /** 产品品牌分类提交 */
    submitFormBrand(){
      this.openCpTypeCodeTow = false;
      this.open = true;
      this.selectedRow = null;
    },


    /**产品分类取消*/
    cancelCpfl() {
      this.openCpTyCodeThree=false;
      this.open = true;
      this.$refs.multipleTableCpfl.clearSelection();
      if (this.title=="新增产品管理"){
        if (this.form.cpfl_name != null){
          this.selectedRow = null;
          this.form.cpfl_name=undefined
        }
      }
    },

    /** 产品分类提交 */
    submitFormCpfl(){
      this.openCpTyCodeThree = false;
      this.open = true;
      if (this.title=='新增产品管理'){
        this.selectedRow = null;
      }
    },

    /** 产品分类修改默认选择 */
    selectRowById(cpflName) {
      // 根据ID选中特定行
      const row = this.sxAttributeValueCpflList.find(item => item.cpflName === cpflName);
      console.log(row)
      if (row) {
        this.$refs.multipleTableCpfl.toggleRowSelection(row);
        this.selectedRow = row;
      }
    },

    /** 计量单位修改默认选择 */
    selectRowDwCode(dwName) {
      // 根据ID选中特定行
      const row = this.jldwMeasurementUnitList.find(item => item.dwName === dwName);
      console.log(row)
      if (row) {
        this.$refs.multipleTableFactory.toggleRowSelection(row);
        this.selectedRow = row;
      }
    },

    /** 物料档案修改默认选择 */
    selectRowWLCode(wlName) {
      // 根据ID选中特定行
      const row = this.wldaMaterialFileList.find(item => item.wlName === wlName);
      console.log(row)
      if (row) {
        this.$refs.multipleTablewlCode.toggleRowSelection(row);
        this.selectedRow = row;
      }
    },


    /** 属性修改默认选择 */
    selectRowSXCode(sxValue) {
      // 根据ID选中特定行
      const row = this.sxAttributeValueList.find(item => item.sxValue === sxValue);
      console.log(row)
      if (row) {
        this.$refs.multipleTableSXCode.toggleRowSelection(row);
        this.selectedRow = row;
      }
    },

    /** 产品品牌修改默认选择 */
    selectRowCpTypeCode(ppName) {
      // 根据ID选中特定行
      const row = this.brandList.find(item => item.ppName === ppName);
      if (row) {
        this.$refs.multipleTableBrand.toggleRowSelection(row);
        this.selectedRow = row;
      }
    },

    /** 查询计量单位列表 */
    getListJLDW() {
      listUnit(this.queryParamsThree).then(response => {
        this.jldwMeasurementUnitList = response.rows;
        this.totalThree = response.total;
      });
    },

    /** 计量单位搜索按钮操作 */
    handleQueryListFactory() {
      this.queryParamsThree.pageNum = 1;
      this.getListJLDW();
    },

    /** 计量单位多选框选中数据 */
    handleSelectionChangeFactory(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTableFactory.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTableFactory.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      if (this.title == "新增产品管理") {
        for (let i = 0; i < selection.length; i++) {
          this.form.dwCode = selection[i].dwCode;
          this.form.dw_name = selection[i].dwName;
        }
      } else {
        for (let i = 0; i < selection.length; i++) {
          this.form.dwCode = selection[i].dwCode;
          this.form.dw_name = selection[i].dwName;
        }
      }
    },

    /**计量单位取消*/
    cancelFactory() {
      this.openDwCodeFact=false;
      this.open = true;
      this.$refs.multipleTableFactory.clearSelection();
      if (this.title=="新增产品管理"){
        if (this.form.dw_name != null){
          this.selectedRow = null;
          this.form.dw_name=undefined
        }
      }
    },

    /** 计量单位提交 */
    submitFormFactory(){
      this.openDwCodeFact = false;
      this.open = true;
      if (this.selectedRow == null){
        this.selectedRow = null;
      }
    },

    /** 物料搜索按钮操作 */
    handleQueryListwlCode() {
      this.queryParamsFour.pageNum = 1;
      this.getListWlMess();
    },

    /** 物料选框选中数据 */
    handleSelectionChangewlCode(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTablewlCode.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTablewlCode.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      if (this.title == "新增产品管理") {
        for (let i = 0; i < selection.length; i++) {
          this.form.wlCode = selection[i].wlCode;
          this.form.wl_name = selection[i].wlName;
        }
      } else {
        for (let i = 0; i < selection.length; i++) {
          this.form.wlCode = selection[i].wlCode;
          this.form.wl_name = selection[i].wlName;
        }
      }
    },

    /**物料取消*/
    cancelWlCode() {
      this.openWlCodeWsk=false;
      this.open = true;
      this.$refs.multipleTablewlCode.clearSelection();
      if (this.title=="新增产品管理"){
        if (this.form.wl_name != null){
          this.selectedRow = null;
          this.form.wl_name=undefined
        }
      }
    },

    /** 物料提交 */
    submitFormWlCode(){
      this.openWlCodeWsk = false;
      this.open = true;
      if (this.selectedRow == null){
        this.selectedRow = null;
      }
    },


    /** 属性选框选中数据 */
    handleSelectionChangeSXCode(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTableSXCode.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTableSXCode.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      if (this.title == "新增产品管理") {
        for (let i = 0; i < selection.length; i++) {
          this.form.sxName = selection[i].sxValue;
        }
      } else {
        for (let i = 0; i < selection.length; i++) {
          this.form.sxName = selection[i].sxValue;
        }
      }
    },

    /**属性取消*/
    cancelSXCode() {
      this.openSXCodeWsk=false;
      this.open = true;
      this.$refs.multipleTableSXCode.clearSelection();
      if (this.title=="新增产品管理"){
        if (this.form.sxName!= null){
          this.selectedRow = null;
          this.form.sxName=undefined
        }
      }
    },

    /** 属性提交 */
    submitFormSXCode(){
      this.openSXCodeWsk = false;
      this.open = true;
      if (this.selectedRow == null){
        this.selectedRow = null;
      }
    },



    /** 产品属性状态修改 */
    handleStatusChange(row) {

      let text = row.isUse === "true" ? "启用" : "停用";

      let isUse = row.isUse === "true" ? "1" : "0";
      this.$modal.confirm('确认要"' + text + '""' + row.cpName + '"吗？').then(function()
      {

        return updateAttributeIsUse(row.cpCode, isUse);

      }).then(() => {

        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {

        row.isUse = row.isUse === "true" ? "1" : "0";
      });
    },

    /** 属性值添加按钮操作 */
    handleAddSxProductAttribute() {
      let obj = {};
      obj.sxTypeCode = "";
      obj.sxName = "";
      this.sxProductAttributeList.push(obj);
    },

    /** 属性值删除按钮操作 */
    handleDeleteSxProductAttribute() {
      if (this.checkedSxProductAttribute.length == 0) {
        this.$modal.msgError("请先选择要删除的属性值数据");
      } else {
        const sxProductAttributeList = this.sxProductAttributeList;
        const checkedSxProductAttribute = this.checkedSxProductAttribute;
        this.sxProductAttributeList = sxProductAttributeList.filter(function(item) {
          return checkedSxProductAttribute.indexOf(item.index) == -1
        });
      }
    },

    handleDblClickRowBrand(row){
      this.openCpTypeCodeTow = false;
      this.open = true;

      this.form.cptypeCode=row.cptypeCode;
      this.form.pp_name = row.ppName;
      this.queryParamsOne.ppName = null;
      this.getListBrand();
    },

    handleDblClickRowCpfl(row) {
      this.openCpTyCodeThree = false;
      this.open = true;

      this.form.cpTypecode = row.cpflCode;
      this.form.cpfl_name = row.cpflName;
      this.queryParamsTwo.cpflName = null;
      this.queryParams.cpTypecode = row.cpflCode;

      listCpflBy(this.queryParams).then(response => {
        this.sxProductAttributeList = response.rows;
      })

    },

    handleDblClickRowJldw(row){
      this.openDwCodeFact = false;
      this.open = true;

      this.form.dwCode = row.dwCode;
      this.form.dw_name = row.dwName;
      this.queryParamsThree.dwName = null;
      this.getListJLDW();
    },

    handleDblClickRowWl(row){
      this.openWlCodeWsk = false;
      this.open = true;

      this.form.wlCode = row.wlCode;
      this.form.wl_name = row.wlName;
      this.queryParamsFour.wlName = null;
      this.getListWlMess();
    },

    handleDblClickRowSXCode(row){

      this.openSXCodeWsk = false;
      this.open = true;

      this.selectedRow.sxValue = row.sxValue;

      this.sxProductAttributeTypeList.push(row.sxCode);
      this.sxProductAttributeTypeListZid.push(row.zId);

      for (let i = 0; i < this.sxProductAttributeList.length; i++) {
        this.sxProductAttributeList[i].sxCode = this.sxProductAttributeTypeList[i];
      }

      for (let i = 0; i < this.sxProductAttributeList.length; i++) {
        this.sxProductAttributeList[i].zId = this.sxProductAttributeTypeListZid[i];
      }

      this.queryParamsFive.sxValue = null;
      this.getListSxCode();
    },
    /** 序号 */
    indexMethod(index) {
      return index + 1 ;
    },
  }
};
</script>
<style>
/*弹出框圆角边框样式*/
/*.el-dialog{
  border-radius: 20px;
}*/
</style>
